package com.ophelisis.common.util;

import java.io.File;
import java.io.IOException;

public class UploadUtil {
	private static String oldFileName = "";
	private static String newFileName = "";
	private static String thumbnailFileName = "";
	
	public static String getOldFileName() {
		return oldFileName;
	}
	
	public static void setOldFileName(String oldFileName) {
		if (oldFileName == "") {
			UploadUtil.oldFileName = oldFileName;
		} else {
			if (UploadUtil.oldFileName == "") {
				UploadUtil.oldFileName = oldFileName;
			} else {
				UploadUtil.oldFileName = UploadUtil.oldFileName + "|" + oldFileName;				
			}
		}
	}
	
	public static String getNewFileName() {
		return newFileName;
	}
	
	public static void setNewFileName(String newFileName) {
		if (newFileName == "") {
			UploadUtil.newFileName = newFileName;
		} else {
			if (UploadUtil.newFileName == "") {
				UploadUtil.newFileName = newFileName;
			} else {
				UploadUtil.newFileName = UploadUtil.newFileName + "|" + newFileName;
			}
		}
	}

	public static String getThumbnailFileName() {
		return thumbnailFileName;
	}

	public static void setThumbnailFileName(String thumbnailFileName) {
		if (thumbnailFileName == "") {
			UploadUtil.thumbnailFileName = thumbnailFileName;
		} else {
			if (UploadUtil.thumbnailFileName == "") {
				UploadUtil.thumbnailFileName = thumbnailFileName;
			} else {
				UploadUtil.thumbnailFileName = UploadUtil.thumbnailFileName + "|" + thumbnailFileName;
			}
		}
	}

	/**
	 * 게시글 첨부파일 이동
	 * 
	 * @param movieCategory
	 */
	public static void fileMoveTo(
			String sourcePath, 
			String targetFolder, 
			String fileName) throws Exception {
		File directory = new File(targetFolder);
		if (!directory.isDirectory()) {
			directory.mkdirs();
		}
		
		try {
			FileUtil.copyBytes(sourcePath + "/" + fileName, targetFolder + "/" + fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		 
		System.gc();
		try {
			File fileObj = new File(sourcePath + "/" + fileName);
			if (fileObj.exists()) {
				fileObj.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
